package cdn_cmd

import (
	"context"
	"fmt"
	"time"

	my_common "atomgit.com/openlinksaas-org/tencloud/common"
	"github.com/spf13/cobra"
	cdn_client "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cdn/v20180606"
	"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
	"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
)

var _queryDataCmd = &cobra.Command{
	Use: "queryData",
	Run: func(cmd *cobra.Command, args []string) {
		if len(args) != 5 {
			my_common.OutputError(fmt.Errorf("缺少域名，指标参数，时间粒度，开始时间和结束时间"))
		}
		domainList := []*string{&args[0]}
		metric := args[1]
		interval := args[2]
		startDay := args[3] + " 00:00:00"
		endDay := args[4] + " 00:00:00"
		client, err := cdn_client.NewClient(common.NewCredential(my_common.CmdSecretId, my_common.CmdSecretKey), my_common.CmdRegion, profile.NewClientProfile())
		if err != nil {
			my_common.OutputError(err)
		}
		ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
		defer cancel()

		req := cdn_client.NewDescribeCdnDataRequest()
		req.Domains = domainList
		req.Metric = &metric
		req.Interval = &interval
		req.StartTime = &startDay
		req.EndTime = &endDay

		res, err := client.DescribeCdnDataWithContext(ctx, req)
		if err != nil {
			my_common.OutputError(err)
		}
		fmt.Println(res.ToJsonString())
	},
}
